package com.qys.livesMall.system.source.controller;


import com.qys.livesMall.common.thread.ThreadContext;
import com.qys.livesMall.common.constants.Constants;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.common.utils.QysStringUtils;
import com.qys.livesMall.framework.view.QysPage;
import com.qys.livesMall.system.source.domain.dto.menu.MenuSearch;
import com.qys.livesMall.system.source.domain.entity.SysMenu;
import com.qys.livesMall.system.source.domain.vo.menu.MenuVO;
import com.qys.livesMall.system.source.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单信息
 */
@RestController
@RequestMapping("/system/menu")
public class SysMenuController {
    @Autowired
    private ISysMenuService menuService;


    /**
     * 菜单列表
     *
     * @param search
     * @return
     */
    @GetMapping("/list")
    public R<List<MenuVO>> list(@RequestBody(required = false) MenuSearch search) {
        return R.ok(menuService.list(search));
    }


    /**
     * 根据菜单编号获取详细信息
     *
     * @param menuId
     * @return
     */
    @GetMapping(value = "/{menuId}")
    public R getInfo(@PathVariable Long menuId) {
        return R.ok(menuService.selectMenuById(menuId));
    }


    /**
     * 新增菜单
     */
    @PostMapping
    @ResponseBody
    public R add(@Validated @RequestBody SysMenu menu) {
        if (!menuService.checkMenuNameUnique(menu)) {
            return R.fail("新增菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        } else if (Constants.YesNo.YES.equals(menu.getIsFrame()) && !QysStringUtils.ishttp(menu.getPath())) {
            return R.fail("新增菜单'" + menu.getMenuName() + "'失败，地址必须以http(s)://开头");
        }
        menu.setCreateBy(ThreadContext.getUserName());
        return R.ok(menuService.insertMenu(menu));
    }

    /**
     * 修改菜单
     */
    @PutMapping
    public R edit(@Validated @RequestBody SysMenu menu) {
        if (!menuService.checkMenuNameUnique(menu)) {
            return R.fail("修改菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        } else if (Constants.YesNo.YES.equals(menu.getIsFrame()) && !QysStringUtils.ishttp(menu.getPath())) {
            return R.fail("修改菜单'" + menu.getMenuName() + "'失败，地址必须以http(s)://开头");
        } else if (menu.getMenuId().equals(menu.getParentId())) {
            return R.fail("修改菜单'" + menu.getMenuName() + "'失败，上级菜单不能选择自己");
        }
        menu.setUpdateBy(ThreadContext.getUserName());
        return R.ok(menuService.updateMenu(menu));
    }

    /**
     * 删除菜单
     */
    @DeleteMapping("/{menuId}")
    public R remove(@PathVariable("menuId") Long menuId) {
        if (menuService.hasChildByMenuId(menuId)) {
            return R.fail("存在子菜单,不允许删除");
        }
        if (menuService.checkMenuExistRole(menuId)) {
            return R.fail(("菜单已分配,不允许删除"));
        }
        return R.ok(menuService.deleteMenuById(menuId));
    }

}
